﻿using System;
using System.Drawing;
using System.Linq;
using SPPropertyBagManagement.Code;

namespace SPPropertyBagManagement.Layouts.SPPropertyBagManagement
{
    public partial class New : PropertyManagementBasePage
    {
        /// <summary>
        /// Represents the method that handles the Init event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!IsPostBack)
            {

            }

            SetPageTitle("New property | " + Scope);

            BindSupportedPropertyValueCodeTypes();
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected new void Page_Load(object sender, EventArgs e)
        {
            trPropertyType.Visible = Scope == ScopeType.List || Scope == ScopeType.ListItem;
        }

        /// <summary>
        /// Binds the supported property value code types.
        /// </summary>
        protected void BindSupportedPropertyValueCodeTypes()
        {
            var types = PropertyManager.GetSupportedPropertyValueCodeTypes();

            var result = from t in types select new { Id = t.ToString(), Name = t.ToString() };

            ddlPropertyType.DataSource = result;
            ddlPropertyType.DataBind();
        }

        /// <summary>
        /// Shows the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="isError">if set to <c>true</c> [is error].</param>
        protected void ShowMessage(string message, bool isError)
        {
            lblMessage.Text = message;

            if (isError)
            {
                lblMessage.ForeColor = Color.Red;
            }
            else
            {
                lblMessage.ForeColor = Color.Silver;
            }

            lblMessage.Visible = true;
        }

        /// <summary>
        /// Hides the message.
        /// </summary>
        protected void HideMessage()
        {
            lblMessage.Visible = false;
        }

        /// <summary>
        /// Handles the OnClick event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveNewProperty();
        }

        /// <summary>
        /// Saves the new property.
        /// </summary>
        private void SaveNewProperty()
        {
            try
            {
                Property myProp = new Property();
                myProp.PropertyName = tbPropertyName.Text.Trim();
                myProp.Scope = Scope;
                myProp.ScopeObjectId = ObjectId;
                myProp.ListItemId = ListItemObjectId;
                myProp.PropertyValue = tbPropertyValue.Text;

                if (Scope == ScopeType.Web || Scope == ScopeType.Farm || Scope == ScopeType.WebApplication)
                {
                    myProp.TypeCode = TypeCode.String;
                }
                else
                {
                    myProp.TypeCode = (TypeCode) Enum.Parse(typeof (TypeCode), ddlPropertyType.SelectedValue);
                }

                myProp.SaveAsNew();

                CloseDialog(0, "saveOk", "Property was saved.");
            }
            catch (Exception ex)
            {
                ShowMessage("Save failed: " + ex.Message, true);
            }
        }
    }
}
